package com.jxt.myjxtmanager.ui.activity;

import android.text.TextUtils;
import android.widget.EditText;

import com.jxt.myjxtmanager.BaseActivity;
import com.jxt.myjxtmanager.R;
import com.jxt.myjxtmanager.db.SaleUser;
import com.jxt.myjxtmanager.db.SaleUserModel;
import com.jxt.myjxtmanager.utils.ToastUtil;

/**
 * Created by qm on 2017/3/14 0014.
 */
public class ChangeUserNameActivity extends BaseActivity {

    private EditText ed_change_username;
    private SaleUser saleUser;


    @Override
    protected int getLayoutId() {
        return R.layout.activity_change_username;
    }

    @Override
    protected void initView() {
        setToolbarTitle("修改用户名");
        setToolbarNavigationIcon(R.drawable.ic_left_return);
        setToolbarTitleRight("完成", getResources().getColor(R.color.white));
        ed_change_username = (EditText) findViewById(R.id.ed_change_username);
    }

    @Override
    public void onClickLeftMenu() {
        super.onClickLeftMenu();
    }

    @Override
    public void onClickRightMenu() {
        if (virfyData()) {
            saleUser.setTrue_name(ed_change_username.getText().toString().trim());
            SaleUserModel.getInstance().update(saleUser);
            ToastUtil.showToast(this, "信息修改成功");
            finish();
        }
    }

    @Override
    protected void initData() {
        saleUser = SaleUserModel.getInstance().loadCurrentSaleUsers();
        if (saleUser != null && !TextUtils.isEmpty(saleUser.getTrue_name())) {
            ed_change_username.setText(saleUser.getTrue_name());
            ed_change_username.setSelection(saleUser.getTrue_name().length());
        }
    }

    private boolean virfyData() {
        if (!TextUtils.isEmpty(ed_change_username.getText().toString().trim()) && saleUser != null) {
            if (TextUtils.equals(ed_change_username.getText().toString().trim(), saleUser.getTrue_name())) {
                ToastUtil.showToast(this, "未检测到修改信息");
                return false;
            } else {
                return true;
            }
        } else {
            ToastUtil.showToast(this, "用户名不能为空");
            return false;
        }
    }
}
